package com.zkyc.framework.website.controller;

import com.zkyc.framework.common.Enum.LogType;
import com.zkyc.framework.common.annotation.Log;
import com.zkyc.framework.common.annotation.Timing;
import com.zkyc.framework.common.exception.ResultCode;
import com.zkyc.framework.common.pojo.Organization;
import com.zkyc.framework.common.vo.ResultVO;
import com.zkyc.framework.website.service.OrganizationService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Organization)表控制层
 *
 * @author libingchuan
 * @since 2022-05-25 16:06:10
 */
@RestController
@RequestMapping("organization")
public class OrganizationController {
    /**
     * 服务对象
     */
    @Resource
    private OrganizationService organizationService;

    /**
     * 查询
     *
     * @return 查询结果
     */
    @Timing
    @GetMapping("list")
    public ResultVO queryList() {
        return organizationService.queryList();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Timing
    @GetMapping("/query/{id}")
    public ResultVO queryById(@PathVariable("id") Integer id) {
        Organization organization = organizationService.queryById(id);
        if (organization == null) {
            return ResultVO.fail(ResultCode.SERVE_ERROR);
        }
        return ResultVO.data(organization);
    }

    /**
     * 新增数据
     *
     * @param organization 实体
     * @return 新增结果
     */
    @PostMapping("save")
    @Log(type = LogType.新增组织)
    public ResultVO add(@RequestBody Organization organization) {
        return organizationService.insert(organization);
    }

    /**
     * 编辑数据
     *
     * @param organization 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    @Log(type = LogType.保存研究院)
    public ResultVO edit(@RequestBody Organization organization) {
        return organizationService.update(organization);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("delete")
    @Log(type = LogType.删除研究院)
    public ResultVO deleteById(Integer id) {
        return organizationService.deleteById(id);
    }

    /**
     * 通过名称查询组织
     *
     * @param name 主键
     * @return 单条数据
     */
    @Timing
    @GetMapping("/queryByName")
    public ResultVO queryByName(String name) {
        return organizationService.queryByName(name);
    }

    @Timing
    @GetMapping("queryList")
    public ResultVO list() {
        return organizationService.list();
    }

    @Timing
    @GetMapping("queryByOrganizationId")
    public ResultVO queryByOrganizationId(Integer organizationId) {
        return organizationService.queryByOrganizationId(organizationId);
    }

    @Timing
    @GetMapping("queryCenterByModuleId")
    public ResultVO queryCenterByModuleId(Integer moduleId) {
        return organizationService.queryCenterByModuleId(moduleId);
    }
}

